﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;
using System.IO;

namespace Site_CamaraTibau
{
    public partial class frmRegimentoInterno : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Page.Form.Attributes.Add("enctype", "multipart/form-data");

                if (!Page.IsPostBack)
                {
                    mtv.SetActiveView(viewListagem);
                    pesquisar();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        #region Contexto
        ModeloContainer bco = new ModeloContainer();
        #endregion

        #region Funções
        public void pesquisar()
        {
            lblErro.Text = string.Empty;
            lblSucesso.Text = string.Empty;

            try
            {

                var objs = (from a in bco.RegimentoInternoes select a).ToList();

                var objs2 = (from a in objs
                             orderby a.Descricao ascending
                             select new
                             {
                                 ID = a.id,
                                 Descricao = a.Descricao,
                             }).ToList();

                if (objs2.Count > 0)
                    btnNovo.Visible = false;
                else
                    btnNovo.Visible = true;

                gvListagem.DataSource = objs2;
                gvListagem.DataBind();
                mtv.SetActiveView(viewListagem);
            }

            catch (Exception ex)
            {
                lblErro.Text = ex.Message.ToString();
            }

        }

        public void salvar()
        {
            lblErro.Text = string.Empty;
            lblSucesso.Text = string.Empty;

            try
            {
                RegimentoInterno obj = new RegimentoInterno();

                if (!string.IsNullOrEmpty(lblID.Text))
                    obj.id = int.Parse(lblID.Text);

                

                obj.Descricao = txtDescricao.Text.Trim();
                obj.OradoresSessao = Convert.ToInt16(txtOradores.Text.Trim());

                if (FileUpload1.HasFile)
                {
                    obj.URL = FileUpload1.FileName;

                    string caminhoDoArquivoCompleto = HttpContext.Current.Server.MapPath("\\Regimento\\" + FileUpload1.FileName);

                    if (File.Exists(caminhoDoArquivoCompleto))
                    {
                        throw new Exception("Já existe um arquivo com este nome!");
                    }
                    else
                    {
                        FileUpload1.SaveAs(caminhoDoArquivoCompleto);
                    }
                }

                RegimentoBO.Salvar(obj);

                lblID.Text = obj.id.ToString();

                lblSucesso.Text = "Registro salvo com sucesso!";
            }
            catch (Exception ex)
            {
                lblErro.Text = ex.Message.ToString();
            }
        }
        public void limpar()
        {
            lblErro.Text = string.Empty;
            lblSucesso.Text = string.Empty;
            lblID.Text = string.Empty;

            txtDescricao.Text = string.Empty;
            txtOradores.Text = string.Empty;
            lkbVisualizar.Visible = false;
        }
        public void carregaForm(int id)
        {
            lblErro.Text = string.Empty;
            lblSucesso.Text = string.Empty;

            try
            {
                RegimentoInterno obj = RegimentoBO.Selecionar(id);

                if (obj != null)
                {
                    lblID.Text = obj.id.ToString();
                    txtDescricao.Text = obj.Descricao;
                    txtOradores.Text = Convert.ToString(obj.OradoresSessao);

                    if (!string.IsNullOrEmpty(obj.URL))
                    {
                        lkbVisualizar.HRef = "~/Regimento/" + obj.URL;
                        lkbVisualizar.Visible = true;
                    }

                    mtv.SetActiveView(viewFormulario);
                }
            }

            catch (Exception ex)
            {
                lblErro.Text = ex.Message.ToString();
            }
        }
        #endregion

        #region Eventos do formulario

        #region Grids

        protected void gvListagem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            lblErro.Text = string.Empty;
            lblSucesso.Text = string.Empty;

            if (e.CommandName.Contains("cmdEditar"))
            {
                int id = Convert.ToInt32(e.CommandArgument);
                carregaForm(id);
            }
        }


        protected void gvListagem_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListagem.PageIndex = e.NewPageIndex;
            pesquisar();
        }

        protected void gvListagem_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#BDD9F6'");
                // when mouse leaves the row, change the bg color to its original value   
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
            }
        }
        #endregion

        #region Botões

        protected void btnNovo_Click(object sender, EventArgs e)
        {
            limpar();
            mtv.SetActiveView(viewFormulario);
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            pesquisar();
            mtv.SetActiveView(viewListagem);
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            salvar();
        }

        protected void btnLimpar_Click(object sender, EventArgs e)
        {
            limpar();
        }

        #endregion


        protected void viewListagem_Activate(object sender, EventArgs e)
        {
            lblHeader.Text = "Pesquisar";
        }

        protected void viewFormulario_Activate(object sender, EventArgs e)
        {
            lblHeader.Text = "Salvar";
        }


        #endregion
    }
}